﻿using System;
using System.Text;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using Tssipl.PaymentAdapter.Core;
namespace Tssipl.PaymentAdapter.MessageFormat.Swift
{
    public class SwiftBlock1 : IBlock
    {
        private char _appID;
        private String _serviceID;
        private String _bic;
        private char _lt;
        private String _branch;
        private String _sessionNo;
        private String _sequence;

        /** Package private constructor that extracts the 
         * block 1 items from the contents (everything between
         * the {1: and the close } non-inclusive) 
         * 
         * @param Contents
         */
        public SwiftBlock1(String Contents)
        {
            _appID = Contents[0];// Contents.charAt(0);
            _serviceID = Contents.SubstringJava(1, 3);
            _bic = Contents.SubstringJava(3, 11);
            _lt = Contents[1]; //Contents.charAt(11);
            _branch = Contents.SubstringJava(12, 15);
            _sessionNo = Contents.SubstringJava(15, 19);
            _sequence = Contents.SubstringJava(19, 25);
        }


        /** Returns the Application Identifier. F is FIN, L is GPA Service message, 
         * and A is GPA system or service message.
         */
        public char getAppID()
        {
            return _appID;
        }


        /** Returns the service identifier which consists of 2 numeric chars. This identifies the type of data being sent or received. 01 is for all GPA, FIN, and user-to-user messages. 21 is message ACK/NACK/UAK/UNK. 03 is a select command.
         * 
         * @return the service ID.
         */
        public String getServiceID()
        {
            return _serviceID;
        }


        /** Returns the 8-character BIC (Bank Identification Code). If the message is an O 
         * message, this is the receiver or if it is an I message, this is the sender.
         * 
         * @return the BIC8 code.
         */
        public String getBIC8()
        {
            return _bic;
        }


        /** Returns the 11-character BIC (Bank Identification Code) which comprises 
         * the 8-character code + Branch code. If the message is an O message, this 
         * is the receiver or if it is an I message, this is the sender.
         * 
         * @return The BIC11 code.
         */
        public String getBIC11()
        {
            return _bic + _branch;
        }


        /** Returns the 12-character BIC (Bank Identification Code) which comprises 
         * the 8-character code + LT + Branch code. If the message is an O message, 
         * this is the receiver or if it is an I message, this is the sender.
         * 
         * @return The BIC12 code.
         */
        public String getBIC12()
        {
            return _bic + _lt + _branch;
        }


        /** Returns the LT (Logical Terminal) of the BIC. If the message 
         * is an O message, this is the receiver or if it is an I message, 
         * this is the sender.
         * 
         * @return The LT.
         */
        public char getLT()
        {
            return _lt;
        }


        /** Returns the 3-character branch code of the BIC. If the message is an O message, this is the receiver or if it is an I message, this is the sender.
         * 
         * @return the Branch code.
         */
        public String getBranch()
        {
            return _branch;
        }


        /** Returns the 4-digit session number in which this message was transmitted.
         * 
         * @return the session number
         */
        public String getSessionNo()
        {
            return _sessionNo;
        }


        /** Returns the ISN or OSN of this message and consists of 6 digits. If the 
         * message is an O message, it is the OSN otherwise it is the ISN for an I message.
         * 
         * @return the ISN/OSN sequence.
         */
        public String getSequence()
        {
            return _sequence;
        }


        /** Returns a properly formatted block 1 including braces and 1: identifier.
         * 
         * @return a complete block 1.
         */
        //@Override
        //HACK public String toString()
        public override String ToString()
        {
            return getFormattedBlock();
        }


        /* (non-Javadoc)
         * @see com.chrislaforetsoftware.swift.message.ISwiftBlock#getContents()
         */
        public String getContents()
        {
            //HACK		StringBuffer sb = new StringBuffer(32);
            StringBuilder sb = new StringBuilder(32);
            sb.Append(_appID);
            sb.Append(_serviceID);
            sb.Append(getBIC12());
            sb.Append(_sessionNo);
            sb.Append(_sequence);
            return sb.ToString();
        }


        /* (non-Javadoc)
         * @see com.chrislaforetsoftware.swift.message.ISwiftBlock#getFormattedBlock()
         */
        public String getFormattedBlock()
        {
            //HACK StringBuffer sb = new StringBuffer(32);
            StringBuilder sb = new StringBuilder(32);
            sb.Append("{1:");
            sb.Append(getContents());
            sb.Append("}");
            return sb.ToString();
        }
    }
}